package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.AdminRoles;
import com.dingreading.cloud.admin.service.AdminRoleMenuService;
import com.dingreading.cloud.admin.service.AdminRoleStaffService;
import com.dingreading.cloud.admin.service.AdminRolesService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DescribeException;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/system/AdminRoles")
@Api(tags = "总部-总部角色功能管理")
public class AdminRolesController {

    @Resource
    private AdminRolesService rolesService;
    @Resource
    private AdminRoleStaffService roleStaffService;
    @Resource
    private AdminRoleMenuService roleMenuService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil
    ) {
        Page<AdminRoles> page = rolesService.pageList(pageUtil);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }


    @ApiOperation(value = "根据ID获取信息")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "角色功能id") @RequestParam(value = "id") Long id
    ) {
        AdminRoles r = this.rolesService.getById(id);
        return R.ok(r);
    }


    @ApiOperation(value = "删除角色功能")
    @DeleteMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "角色功能id") @RequestParam(value = "id") Long id
    ) {
        AdminRoles data = rolesService.getById(id);
        if (data == null) return R.fail("菜单获取失败");

        int l = rolesService.dropBean(id);
        if (l > 0) {
            roleStaffService.dropByRoleId(id);
            roleMenuService.dropByRoleId(id);
        } else {
            return R.fail("删除失败");
        }
        return R.ok();
    }

    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "添加/修改角色功能")
    @PostMapping(value = "/newBean", produces = "application/json; charset=UTF-8")
    public R<Object> newBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "角色功能id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "角色功能名") @RequestParam(value = "name") String name,
            @ApiParam(value = "角色标识") @RequestParam(value = "identify", required = false) String identify,
            @ApiParam(value = "备注") @RequestParam(value = "remark", required = false) String remark
    ) {
        if (id == null || id <= 0) {
            AdminRoles data = new AdminRoles();
            data.setName(name);
            data.setIdentify(identify);
            data.setRemark(remark);
            rolesService.saveOrUpdate(data);
        } else {
            AdminRoles data = rolesService.getById(id);
            if (data == null) return R.fail("菜单获取失败");

            data.setName(name);
            data.setIdentify(identify);
            data.setRemark(remark);
            rolesService.saveOrUpdate(data);
        }
        return R.ok();
    }

}
